function [dest_img fuse_img thresh_img] = ecen5283_edge_detect_matched(src_img, sigma, hsize, group_num)

[rows cols] = size(src_img);
src_img = double(src_img);
%%General Gaussian filter group
    function g = gen_gauss(sigma, haftsize)
        gsize = 2 * haftsize + 1;
        g = zeros(gsize, gsize);
        for i=1:gsize
            for j=1:gsize
                u = i - haftsize - 1;
                g(i,j) = - 1/(sqrt(2*pi)*sigma) * exp(-u^2/(2*sigma^2));
%                 g(i,j) = - exp(-u^2/(2*sigma^2));
            end            
        end
        g = g - mean(g(:));
        haft_padded_size = ceil(sqrt(2)*haftsize) - haftsize;
        g = padarray(g, [haft_padded_size haft_padded_size], 0, 'both');
    end
    function G = gen_gauss_group(sigma, haftsize, amount)
        angle = floor(180/amount);
        G(:, :, 1) = gen_gauss(sigma, haftsize);
        for k = 2:amount
            G(:, :, k) = imrotate(G(:, :, k-1), angle, 'bicubic', 'crop');
        end        
    end
G = gen_gauss_group(sigma, hsize, group_num);

%% Apply the kernel
for n = 1:group_num
    I(:, :, n) = imfilter(src_img,G(:, :, n), 'conv');
%     figure;imshow(I(:,:,n),[]);
end

%% Fuse
temp_img = zeros(rows, cols);
for x = 1:rows
    for y = 1:cols
        temp_img(x,y) = max(I(x,y,:));
    end
end
fuse_img = temp_img;
%% Threshold
% level = graythresh(temp_img);
% dest_img = im2bw(temp_img, level);
thresh = quantile(temp_img(:), 0.9);
dest_img = temp_img;
dest_img(temp_img >= thresh) = 1;
dest_img(temp_img < thresh) = 0;
thresh_img = dest_img;

%%Post-processing
dest_img = im2bw(dest_img);
dest_img = remove_isolated_point(dest_img);
dest_img = bwmorph(dest_img,'thin');
end